#include "ClientGameManager.h"

ClientGameManager::ClientGameManager()
{
}

void ClientGameManager::SlotPlayerList(PlayerlistBean* playerlist_bean)
{
    m_game->SetPlayers(playerlist_bean->Players());
}

void ClientGameManager::SlotMap(MapBean* map_bean)
{
    m_game->SetSettings(map_bean->GetSettings());
    m_game->GameBoard()->SetMap(map_bean->GetMap());
}

void ClientGameManager::SlotSnapshot(SnapshotBean* snapshot_bean)
{
    m_game->GameBoard()->SetTanks(snapshot_bean->TankList());
    m_game->GameBoard()->SetBullets(snapshot_bean->BulletList());
}

void ClientGameManager::GameLoop()
{
    qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
    qint64 timeElapsed = m_lastUpdate - currentTime;
    m_lastUpdate = currentTime;
    double secondsElapsed = timeElapsed / 1000;


    GameBoardPtr boardPtr = m_game->GameBoard();

    QList<BulletPtr> bulletList = *boardPtr->Bullets();
    BulletPtr bullet;
    foreach(bullet, bulletList)
    {
        bullet->moveFor(calcObjectPosition(bullet.data(), secondsElapsed));
        bullet->UpdateImagePosition();
    }

    QList<TankPtr> tankList = *boardPtr->Tanks();
    TankPtr tank;
    foreach(tank, tankList)
    {
        tank->moveFor(calcObjectPosition(tank.data(), secondsElapsed));
        tank->UpdateImagePosition();
    }
}
